﻿using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MvcCodeNotes.Models
{
    public class SP_Pagination
    {
        private string _tableName;
        private string _fields = "*";
        private string _orderField;
        private int _pageSize = 20;
        private int _pageIndex = 1;
        private bool _sortAsc;
        private string _strWhere;

        public string TableName
        {
            set { _tableName = value; }
            get { return _tableName; }
        }

        public string Fields
        {
            set { _fields = value; }
            get { return _fields; }
        }      
        public int PageSize
        {
            set { _pageSize = value; }
            get { return _pageSize; }
        }
        public int PageIndex
        {
            set { _pageIndex = value; }
            get { return _pageIndex; }
        }
        public string OrderField
        {
            set { _orderField = value; }
            get { return _orderField; }
        }
        public bool SortAsc
        {
            set { _sortAsc = value; }
            get { return _sortAsc; }
        }
        public string strWhere
        {
            set { _strWhere = value; }
            get { return _strWhere; }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(string TableName, string Fields, string OrderField, int PageSize, int PageIndex, bool SortAsc, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@strGetFields", SqlDbType.VarChar, 500),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),                    
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    new SqlParameter("@Total", SqlDbType.Int)
                    };
            parameters[0].Value = TableName;
            parameters[1].Value = Fields;
            parameters[2].Value = OrderField;
            parameters[3].Value = PageSize;
            parameters[4].Value = PageIndex;
            parameters[5].Value = SortAsc;
            parameters[6].Value = strWhere;
            parameters[7].Value = 0;
            return DbHelperSQL.RunProcedure("SP_Pagination", parameters, "ds");
        }

        public DataSet GetList()
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@strGetFields", SqlDbType.VarChar, 500),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),                    
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    new SqlParameter("@Total", SqlDbType.Int)
                    };
            parameters[0].Value = _tableName;
            parameters[1].Value = _fields;
            parameters[2].Value = _orderField;
            parameters[3].Value = _pageSize;
            parameters[4].Value = _pageIndex;
            parameters[5].Value = _sortAsc;
            parameters[6].Value = _strWhere;
            parameters[7].Value = 0;
            return DbHelperSQL.RunProcedure("SP_Pagination", parameters, "ds");
        }
    }
}